package com.example.securitydemo.controller;

import com.aaa.entity.TabMenu;
import com.aaa.entity.TabRole;
import com.aaa.entity.TabUser;
import com.aaa.util.Result;
import com.example.securitydemo.api.UserApiController;
import com.example.securitydemo.service.ITabMenuService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 于永利
 * @since 2023-07-14
 */
@RestController
@RequestMapping("/tab-menu")
public class TabMenuController {

    @Resource
    private ITabMenuService menuService;
    @Resource
    private UserApiController userApiController;

    // 树形菜单使用的信息
    @GetMapping
    public Result getAllMenu(){
        // 查询所有的菜单的i的以及名字
        List<TabMenu> menus=menuService.getAllTreeMenu();
        return new Result(200,"成功",menus);
    }

    /**
     * 根据登录的用户的信息 查询对应的资源信息
     */
    @GetMapping("getPremission")
    public Result getResource(){

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) authentication.getAuthorities();

        List<String> menus=authorities.stream().map(simpleGrantedAuthority -> simpleGrantedAuthority.getAuthority()).collect(Collectors.toList());
        return Result.success(menus);


    }

}
